package com.jeeplus.modules;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.modules.web.im.EquipmentIoPkg;

import java.io.*;
import java.net.Socket;


public class SocketClientDemo implements Runnable{

    public static final byte[] init = "{\"code\":\"0\",\"data\":\"\",\"msg\":\"success\",\"equipment\":\"233\"}".getBytes();


    private Socket socket;
    private InputStream ois;
    private DataOutputStream writer;
    public SocketClientDemo(){
        try{
            //127.0.0.1表示本机IP，10000为服务器Socket设置的端口
            socket = new Socket("127.0.0.1", 14000);
            ois = socket.getInputStream();
            writer = new DataOutputStream(socket.getOutputStream());
            writer.write(init);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void run(){
        try{
            //这里就可以读取所有行String
            while (true) {
                byte[] buf = new byte[10240];
                int length = 0;
                length = ois.read(buf);
                String str = new String(buf, 0, length);
                writer.write(str.getBytes());
                System.out.println(str);
            }

        }catch(IOException e){
            e.printStackTrace();
            System.out.println("problem");
        }finally{
            //最后关闭Socket
            try{
                if(socket!=null)socket.close();
                if(ois!=null)ois.close();
                if(writer!=null)writer.close();
            }catch(IOException e){
                e.printStackTrace();
            }

        }

    }
    public static void main(String[] args){
        new Thread(new SocketClientDemo()).start();
    }
}
